﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace QA.WindowsForms
{
    public partial class frmDownloader : Form
    {
        public frmDownloader()
        {
            InitializeComponent();
        }

        private int rowCount = 0;

        private Button getBaseButton()
        {
            Button baseButton = new Button();
            baseButton.Anchor = System.Windows.Forms.AnchorStyles.None;
            baseButton.Size = new System.Drawing.Size(24, 24);
            baseButton.TabIndex = 1;
            baseButton.UseVisualStyleBackColor = true;
            return baseButton;
        }
        private Button getBrowse(string fileName)
        {
            Button browse = getBaseButton();
            browse.Image = global::WindowsForms.Images.Browse1;
            browse.Click += delegate(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start(Path.GetDirectoryName(fileName));
            };
            return browse;
        }
        private Button getStatusButton(WebClient downer, string fileName)
        {
            Button stsButton = getBaseButton();
            stsButton.Image = global::WindowsForms.Images.btnClose1;
            stsButton.Click += delegate(object sender, EventArgs e)
            {
                if (downer.IsBusy)
                {
                    downer.CancelAsync();
                    stsButton.Enabled = false;
                }
            };
            downer.DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e)
            {
                stsButton.Image = global::WindowsForms.Images.btnDone;
            };

            return stsButton;
        }
        private ProgressBar getProgressBar()
        {
            ProgressBar pgb = new ProgressBar();
            pgb.Value = 0;
            pgb.Maximum = 100;
            pgb.Dock = DockStyle.Fill;
            return pgb;
        }
        private Label getLabel()
        {
            Label lbl = new Label();
            lbl.Anchor = AnchorStyles.Left;
            lbl.AutoSize = true;
            return lbl;
        }
        private void addControlsToTable(Label lbl, ProgressBar pgb, Button stsButton, Button browse)
        {
            if (rowCount >= tblMain.RowCount)
            {
                tblMain.RowCount++;
                this.tblMain.RowStyles.Add(new RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
                this.tblMain.Size = new Size(452, tblMain.Height + 30);
            }
            tblMain.Controls.Add(lbl, 0, rowCount);
            tblMain.Controls.Add(pgb, 1, rowCount);
            tblMain.Controls.Add(stsButton, 2, rowCount);
            tblMain.Controls.Add(browse, 3, rowCount);
            ++rowCount;
        }

        public void Download(string fileName, string url)
        {
            Download(new WebClient(), fileName, url);
        }
        public void Download(WebClient downer, string fileName, string url)
        {
            ProgressBar pgb = getProgressBar();
            Label lbl = getLabel();
            lbl.Text = Path.GetFileName(fileName);
            addControlsToTable(lbl, pgb, getStatusButton(downer, fileName), getBrowse(fileName));
            downer.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
            {
                pgb.Value = e.ProgressPercentage;
            };
            downer.DownloadFileAsync(new Uri(url), fileName);
        }
    }
}
